package com.store.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.easeui.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.MemberInfoBean;

import java.util.List;


public class MemberListAddContactAdapter extends BaseAdapter {
    private Context context;        //上下文对象引用
    private List<MemberInfoBean> memberInfoBeanList;

    public MemberListAddContactAdapter(Context context, List<MemberInfoBean> memberInfoBeanList) {
        this.context = context;
        this.memberInfoBeanList = memberInfoBeanList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return memberInfoBeanList.size();
    }


    @Override
    public MemberInfoBean getItem(int position) {
        return memberInfoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MemberInfoBean memberInfoBean = memberInfoBeanList.get(position);
        convertView = LayoutInflater.from(context).inflate(R.layout.item_contact_list, null);
        TextView tv_nick_name = (TextView) convertView.findViewById(R.id.tv_nick_name);
        final TextView checkBox = (TextView) convertView.findViewById(R.id.cb);
        CircleImageView circleImageView = (CircleImageView) convertView.findViewById(R.id.avatar);

//        checkBox.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                onClick.clickCheckBox(position);
//            }
//        });



        if (!TextUtils.isEmpty(memberInfoBean.getNick_name())) {
            tv_nick_name.setText(memberInfoBean.getNick_name());
        } else {
            tv_nick_name.setText("匿名用户");
        }

        if (memberInfoBean.isSelect()) {
            checkBox.setBackgroundResource(R.drawable.img_card_on);
        } else {
            checkBox.setBackgroundResource(R.drawable.img_card_off);
        }
        String url = memberInfoBean.getHead_pic_path();
        if (TextUtils.isEmpty(url)) {
            url = "null";
        }
        Log.i("mylog"," aa "+url);
        ImageLoader.getInstance().displayImage(url, circleImageView, options);
        if (memberInfoBean.getNick_name().equals("全部")) {
            circleImageView.setVisibility(View.GONE);
        } else {
            circleImageView.setVisibility(View.VISIBLE);
        }
        Log.v("zyl", "memberInfoBean.getNick_name()：" + memberInfoBean.getNick_name());
        Log.v("zyl", "memberInfoBean.isSelcet()：" + memberInfoBean.isSelect());
        return convertView;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.ease_default_avatar)
            .showImageOnFail(R.drawable.ease_default_avatar)
            .showStubImage(R.drawable.ease_default_avatar)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    public interface OnClick {
        void clickCheckBox(int position);
    }

}
